package cn.com.wangluotongxin.admin.netty.config;

import cn.com.wangluotongxin.admin.netty.constants.AdminNettyRabbitMqConstants;
import cn.com.wangluotongxin.admin.netty.property.AdminNettyRabbitmqProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class AdminNettyRabbitMqConfig {

    @Autowired
    private AdminNettyRabbitmqProperty rabbitmqProperty;

    @Bean(name = AdminNettyRabbitMqConstants.ADMIN_NETTY_EXCHANGE_NAME)
    DirectExchange loadAdminNettyExchange() {
        String exchangeName = AdminNettyRabbitMqConstants.ADMIN_NETTY_EXCHANGE;
        if(!rabbitmqProperty.getEnabled()) {
            log.info("未启用admin netty rabbitmq交换机:exchange name-{}", exchangeName);
            return null;
        }
        log.info("初始化默认admin netty rabbitmq交换机:exchange name-{}", exchangeName);
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl" , 60*1000); // 消息过期时间 60秒
        return new DirectExchange(exchangeName, true, false, arguments);
    }
}
